<template>
	<view class="content">
		<view v-if="todolist.length !== 0">
			<view class="todo-header">
				<view class="todo-header-left">
					<text class="active-text">全部</text>
					<text>{{num}}条</text>
				</view>
				<view class="todo-header-right">
					<view v-for="(item, index) in list"
						  class="todo-header-right-item"
						  :class="index === itemIndex ? 'active-tab': ''"
						  @click="clickAll(index)"
					>{{item}}</view>
				</view>
			</view>
			<view class="todo-content">
				<!-- todo-finish -->
				<view class="todo-list"
					  :class="item.checked ? 'todo-finish': ''"
					  v-for="(item, index) in prelist" 
					  @click="finish(item, index)"
				>
					<view class="todo-list-checkbox">
					</view>
					<view class="todo-list-content">{{item.value}}</view>
				</view>
			</view>
		</view>
		<view v-else>
			<view class="image-default">
				<image src="../../static/default.png" mode="aspectFit"></image>
			</view>
			<view class="default-info">
				<view class="default-info-text">您还没有创建任何待办事项</view>
				<view class="default-info-text">点击下方+号创建一个吧</view>
			</view>
		</view>
		<view class="create-todo" @click="clickAdd">
			<image :src="isShow ? '../../static/cha.png':'../../static/add.png'"></image>
		</view>
		<view class="create-content" v-if="isShow">
			<view class="create-input">
				<input type="text" value="" v-model="val" placeholder="请输入您要创建的todo" />
			</view>
			<view class="create-button" @click="addList">
				创建
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				itemIndex: 0,
				isShow: false,
				list: ["全部", "待办", "已完成"],
				todolist: [],
				val: '',
				num: 0
			}
		},
		onLoad() {

		},
		computed: {
			prelist() {
				let itemIndex = this.itemIndex
				let todolist = this.todolist
				if(itemIndex === 0) {
					this.num = todolist.length
					return todolist
				} else if(itemIndex === 1) {
					let list = []
					for(let i = 0; i < todolist.length; i++) {
						if(!todolist[i].checked) {
							list.push(this.todolist[i])
						}
					}
					this.num = list.length
					return list
				} else {
					let list = []
					for(let i = 0; i < todolist.length; i++) {
						if(todolist[i].checked) {
							list.push(this.todolist[i])
						}
					}
					this.num = list.length
					return list
				}
			}
		},
		methods: {
			clickAll(index) {
				this.itemIndex = index
			},
			clickAdd() {
				this.isShow = !this.isShow
			},
			addList() {
				if(this.val) {
					let obj = {
						checked: false,
						value: this.val,
					}
					this.todolist.push(obj)
					this.val = ''
					this.isShow = !this.isShow
				} 
			},
			finish(item, index) {
				this.todolist[index].checked = !item.checked
			}
		}
	}
</script>

<style>
	.todo-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 45px;
		font-size: 12px;
		box-shadow: -1px 1px 5px 0 rgba(0, 0, 0, 0.1);
		padding: 0 15px;
	}
	.todo-header-left text {
		padding: 0 5px;
	}
	.todo-header-right {
		display: flex;
	}
	.todo-header-right-item {
		padding: 0 5px;
	}
	.active-tab {
		color: #279abf;
	}
	.active-text {
		color: #279abf;
		font-size: 14px;
	}
	.todo-list {
		margin: 10px 15px;
		padding: 15px;
		background-color: #d7edfd;
		border-radius: 10px;
		box-shadow: -1px 1px 5px 0 rgba(0, 0, 0, 0.1);
		font-size: 14px;
		border-left: 5px solid #a5d4e0;
	}
	.todo-list-checkbox {
		width: 20px;
		height: 20px;
		border-radius: 50%;
		background-color: #ffffff;
		box-shadow: -1px 1px 5px 0 rgba(0, 0, 0, 0.1);
		float: left;
	}
	.todo-list-content {
		margin-left: 30px;
		width: 100%;
	}
	.todo-finish {
		border-left: 5px solid #cac8d2;
	}
	.todo-finish .todo-list-checkbox {
		background-color: #cac8d2;
	}
	.todo-finish .todo-list-content {
		color: #999999;
		text-decoration:line-through;
	}
	.create-todo {
		position: fixed;
		bottom: 20px;
		left: 50%;
		transform: translateX(-50%);
		width: 50px;
		height: 50px;
		border-radius: 50%;
		background-color: #e3f0f5;
		box-shadow: -1px 1px 5px 2px rgba(0, 0, 0, 0.1);
	}
	.create-todo image {
		width: 30px;
		height: 30px;
		margin: 10px;
	}
	.create-content {
		height: 40px;
		position: fixed;
		bottom: 95px;
		left: 15px;
		right: 15px;
		padding-left: 15px;
		background-color: #e2f1f6;
		border-radius: 20px;
		font-size: 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-shadow: -1px 1px 5px 2px rgba(0, 0, 0, 0.1);
	}
	.create-input {
		color: #a7d4df;
	}
	.create-button {
		height: 40px;
		border-radius: 20px;
		box-shadow: -1px 1px 5px 2px rgba(0, 0, 0, 0.1);
		padding: 10px 15px;
		box-sizing: border-box;
		color: #a7d4df;
	}
	.create-content:after {
		content: '';
		position: absolute;
		right: 0;
		left: 0;
		bottom: -8px;
		margin: 0 auto;
		width: 10px;
		height: 10px;
		background-color: #deeff5;
		transform: rotate(45deg); 
	}
	.default {
		padding-top: 100px;
	}

	.image-default {
		display: flex;
		justify-content: center;
	}

	.image-default image {
		width: 100%;
	}

	.default-info {
		text-align: center;
		font-size: 14px;
		color: #CCCCCC;
	}
</style>
